﻿//
//  GNS_SceneMgr.cpp
//  MobileManage
//  FuncTion 场景图层管理类
//  Created by mr.l on 16/1/25.
//
//

#include "GNS_SceneMgr.h"
#include "GNS_HallScene.h"
#include "GNS_LoginScene.h"
USING_NS_CC;

Scene * GNS_SceneMgr::replaceScene(SCENE_MOBILE sceneVal)
{
    auto scene = _mapScene.at(sceneVal);
    if (scene==nullptr) {
        switch (sceneVal) {
            case LOGIN_SCENE:
            {
                scene = GNS_LoginScene::createScene();
                _mapScene.insert(LOGIN_SCENE, scene);
                break;
            }
            case HALL_SCENE:
            {
                scene = GNS_HallScene::createScene();
                _mapScene.insert(HALL_SCENE, scene);
            }
            default:
                break;
        }
    }
    auto transition=TransitionPageTurn::create(2.0f,scene,false);
    Director::getInstance()->replaceScene(transition);
    if (_curScene !=nullptr && scene!=_curScene) {
        std::vector<int> mapKeyVec;
        mapKeyVec = _mapScene.keys();//获取所有 Key 值
        for(auto key:mapKeyVec)
        {
            auto selscene = _mapScene.at(key);
            if (selscene==_curScene) {
                selscene =nullptr;
                _mapScene.erase(key);
                break;
            }
        }
    }
    _curScene =scene;
    return _curScene;
}
Layer * GNS_SceneMgr::AddSceneLayer(LAYER_MOBILE layerVal)
{
    auto layer = _mapLayer.at(layerVal);
    if (layer == nullptr) {
        switch (layerVal) {
            case HALL_LAYER:
            {
                layer = GNS_HallScene::create();
                _mapLayer.insert(HALL_LAYER, layer);
            }
                break;
            case LOGIN_LAYER:
            {
                layer = GNS_LoginScene::create();
                _mapLayer.insert(LOGIN_LAYER, layer);
            }
                break;
            default:
                break;
        }
    }
    if (_curLayer !=nullptr && layer!=_curLayer) {
        std::vector<int>mapKeyVec;
        mapKeyVec = _mapLayer.keys();
        for (auto key:mapKeyVec) {
            auto sellayer = _mapLayer.at(key);
            if (sellayer ==_curLayer) {
                _mapLayer.erase(key);
                break;
            }
        }
    }
    
    _curLayer =layer;
    return _curLayer;
}

GNS_CursorTextField * GNS_SceneMgr::MycreateEditBox(std::string tips, Point pos, float FontSize, Color3B color, int limit, bool encryption)
{
    GNS_CursorTextField* editbox = GNS_CursorTextField::textFieldWithPlaceHolder(tips.c_str(), "", FontSize);
    editbox->setColor(color);
    editbox->setAnchorPoint(Vec2(0, 0.5f));
    editbox->setPosition(pos);
    editbox->setHorizontalAlignment(TextHAlignment::LEFT);
    editbox->initCursorSprite(FontSize, color);
    //editbox->AddCursor(rootNode);
    editbox->setLimitNum(limit);
    editbox->setPasswordEnabled(encryption);
    return editbox;
}
